<?php
class RegistrationsController extends AppController {

    var $name = 'Registrations';
    var $helpers = array('Html', 'Form', 'Javascript');
    var $components = array('Email');

    function beforeFilter() {
        $this->Auth->allow('add');
        $this->Auth->allow('resume');
        $this->Auth->allow('edit');
        $this->Auth->allow('thank_you');
    }

    function resume() {
        $this->Session->destroy();
        if (!empty($this->data)) {
            $registration = $this->Registration->find('first', array('conditions'=>array('Registration.email'=>$this->data['email'], 'Registration.resumption_key'=>$this->data['resumption_key'])));
            if (!empty($registration)) {
                $this->Session->write('registration_id', $registration['Registration']['id']);
                $this->Session->write('resumption_key', $registration['Registration']['resumption_key']);
                $this->redirect(array('action'=>'edit', $registration['Registration']['id']));
            }
            else {
                $this->Session->setFlash('Email or resumption key invalid.');
            }
        }
    }

    function index() {
        $this->Registration->recursive = 1;

        $this->set('registrations', $this->Registration->find('all'));
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid Registration.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('registration', $this->Registration->read(null, $id));
    }

   /*function add() {
      if (!empty($this->data)) {
         $this->Registration->create();
         if ($this->Registration->save($this->data)) {
            $this->Session->setFlash(__('The Registration has been saved', true));
            $this->redirect(array('action'=>'index'));
         } else {
            $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
         }
      }
      $tournaments = $this->Registration->Tournament->find('list');
      $this->set(compact('tournaments'));
   }*/

    function add() {
        if(!empty($this->data)) {
            $this->data['Registration']['resumption_key'] = substr(md5(uniqid(rand(), true)), 0, 12);
            if ($this->Registration->saveAll($this->data, array('validate'=>'first'))) {
                $this->Session->setFlash(__('The Registration has been saved', true));
                $this->Session->write('resumption_key', $this->data['Registration']['resumption_key']);
                $this->redirect(array('action'=>'thank_you'));
            } else {
                $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
            }
        }
        $tournaments = $this->Registration->Tournament->find('list');
        $this->set(compact('tournaments'));
    }

    function edit($id = null) {
        if (!($this->Session->read('registration_id') == $id)) {
            if (!$this->Session->check('Auth.User.id')) {
                $this->redirect(array('action'=>'resume'));
            }
        }
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Registration', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            $registration = $this->Registration->findById($id);
            $this->data['Registration']['resumption_key'] = $registration['Registration']['resumption_key'];

            $this->Registration->deleteAll(array('Registration.id' => $id));
            if ($this->Registration->saveAll($this->data, array('validate'=>'first'))) {
                if ($this->Session->check('Auth.User.id')) {
                    $this->redirect(array('action'=>'index'));
                }
                else {
                    $registration = $this->Registration->findById($id);
                    $this->redirect(array('action'=>'thank_you'));
                }
            } else {
                $this->Session->setFlash('The Registration could not be saved. Please, try again.');
                $this->Session->destroy();
            }
        }
        if (empty($this->data)) {

            $this->data = $this->Registration->findById($id);
            $this->set('registration',$this->data);
            $this->set('registration_id', $id);
        }
        $tournaments = $this->Registration->Tournament->find('list');
        $this->set(compact('tournaments'));
    }

    function thank_you() {
        if (!$this->Session->check('resumption_key')) {
            $this->redirect(array('action'=>'add'));
        }

        $this->set('resumption_key', $this->Session->read('resumption_key'));
        $registration = $this->Registration->find('all',
            array('conditions'=>
                array('Registration.resumption_key'=>$this->Session->read('resumption_key'))));
        $this->set('registration', $registration[0]);

        //send an email summary
        $this->Email->smtpOptions = array(
            'port'=>'465', 
            'timeout'=>'30',
            'host' => 'ssl://mail.fmquizzing.net',
            'username'=>'registration@fmquizzing.net',
            'password'=>'fmqu7435'
        );
        $this->Email->delivery = 'smtp';
        $this->Email->to       = 'Head Coach <' . $registration[0]['Registration']['email'] . '>';
        $this->Email->subject  = 'Quiz Registration Confirmation';
        $this->Email->replyTo  = 'registration@fmquizzing.net';
        $this->Email->from     = 'Webmaster <registration@fmquizzing.net>';
        $this->Email->sendAs   = 'text';
        $this->Email->template = 'confirmation';

        $this->Email->send();

        $this->set('smtperrors', $this->Email->smtpError);

        $this->Session->destroy();
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Registration', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->Registration->del($id)) {
            $this->Session->setFlash(__('Registration deleted', true));
            $this->redirect(array('action'=>'index'));
        }
    }


    function admin_index() {
        $this->Registration->recursive = 0;
        $this->set('registrations', $this->paginate());
    }

    function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid Registration.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('registration', $this->Registration->read(null, $id));
    }

    function admin_add() {
        if (!empty($this->data)) {
            $this->Registration->create();
            if ($this->Registration->save($this->data)) {
                $this->Session->setFlash(__('The Registration has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
            }
        }
        $tournaments = $this->Registration->Tournament->find('list');
        $this->set(compact('tournaments'));
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Registration', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Registration->save($this->data)) {
                $this->Session->setFlash(__('The Registration has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Registration->read(null, $id);
        }
        $tournaments = $this->Registration->Tournament->find('list');
        $this->set(compact('tournaments'));
    }

    function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Registration', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->Registration->del($id)) {
            $this->Session->setFlash(__('Registration deleted', true));
            $this->redirect(array('action'=>'index'));
        }
    }

}
?>
